import datetime
from rest_framework import serializers, status

from databases.models import *
from rest_framework.response import Response


class PostSendMessageSerializer(serializers.ModelSerializer):
    """
    用户注册
    """
    phone = serializers.CharField(label="手机号码", write_only=True)

    class Meta:
        model = AppUser
        fields = (
            'phone',
        )


class PostRegisterSerializer(serializers.ModelSerializer):
    """
    用户注册
    """
    phone = serializers.CharField(label="手机号码", write_only=True)
    password = serializers.CharField(label="密码", write_only=True)
    code = serializers.CharField(label="手机验证码", write_only=True)

    class Meta:
        model = AppUser
        fields = (
            'phone', 'password', 'code',
        )


class PostThirdLoginSerializer(serializers.ModelSerializer):
    """
    用户注册
    """
    TYPE_CHOICE = ((0, 'qq'),
                   (1, '微信'),
                   (2, '微博'),
                   )

    open_id = serializers.CharField(label="第三方登录openid", write_only=True)
    type = serializers.ChoiceField(label="第三方登录类型", write_only=True, choices=TYPE_CHOICE)

    class Meta:
        model = AppUser
        fields = (
            'open_id', 'type',
        )

        # def validate(self, attrs):
        #     _openid = attrs['open_id']
        #     # _openid = _openid.decode('utf-8')
        #     _judge = None
        #     for ch in _openid:
        #         if u'\u4e00' <= ch <= u'\u9fff':
        #             _judge = True
        #             raise Exception('登录失败，请重试')
        #         else:
        #             pass


class PostLoginSerializer(serializers.ModelSerializer):
    """
    用户注册
    """
    phone = serializers.CharField(label="手机号码", write_only=True)
    password = serializers.CharField(label="密码", write_only=True)

    class Meta:
        model = AppUser
        fields = (
            'phone', 'password',
        )


class PostForgetSerializer(serializers.ModelSerializer):
    """
    用户注册
    """
    code = serializers.CharField(label="短信验证码", write_only=True)
    phone = serializers.CharField(label="手机号", write_only=True)
    password = serializers.CharField(label="新密码", write_only=True)

    class Meta:
        model = AdminUser
        fields = (
            'password', 'code', 'phone'
        )


class PostResetSerializer(serializers.ModelSerializer):
    """
    用户注册
    """
    old = serializers.CharField(label="旧密码", write_only=True)
    new = serializers.CharField(label="新密码", write_only=True)

    class Meta:
        model = AdminUser
        fields = (
            'old', 'new',
        )


class PostLoginCodeSerializer(serializers.ModelSerializer):
    """
    用户注册
    """
    phone = serializers.CharField(label="手机号码", write_only=True)
    code = serializers.CharField(label="验证码", write_only=True)

    class Meta:
        model = AppUser
        fields = (
            'phone', 'code',
        )


class PostSaveUserInfoSerializer(serializers.ModelSerializer):
    """
    用户注册
    """
    SEX_CHOICE = (
        (0, '男'),
        (1, '女'),
    )
    nick_name = serializers.CharField(label="昵称", write_only=True)
    portrait = serializers.CharField(label="头像", write_only=True)
    sex = serializers.ChoiceField(label="性别", write_only=True, choices=SEX_CHOICE)
    height = serializers.IntegerField(label="身高", write_only=True)
    weight = serializers.IntegerField(label="体重", write_only=True)
    target_weight = serializers.IntegerField(label="目标体重", write_only=True)
    year = serializers.IntegerField(label="出生年", write_only=True, max_value=datetime.datetime.now().year,
                                    min_value=1900)
    month = serializers.IntegerField(label="出生月", write_only=True, max_value=12, min_value=1)
    city = serializers.CharField(label="所在城市", write_only=True)

    class Meta:
        model = UserInfo
        fields = (
            'nick_name', 'sex', 'height', 'weight', 'year', 'month', 'city', 'portrait', 'target_weight',
        )

        # def validate(self, attrs):
        #     _aaa = super().validate(attrs)
        #     try:
        #         _year = attrs['year']
        #         _month = attrs['month']
        #         if _year == datetime.datetime.now().year:
        #             if _month > datetime.datetime.now().month:
        #                 _aaa['exception'] = '出生月还未到'
        #                 return _aaa
        #             else:
        #                 return _aaa
        #         else:
        #             return _aaa
        #     except Exception as e:
        #         return e


class PostReleaseMessageSerializer(serializers.ModelSerializer):
    text = serializers.CharField(label="动态文字", write_only=True)
    photo = serializers.CharField(label="动态图片", write_only=True)
    present = serializers.FloatField(label="当前体重", write_only=True)

    class Meta:
        model = AffairsRecord
        fields = (
            'text', 'photo', 'present',
        )


class PostCreateCommentSerializer(serializers.ModelSerializer):
    father = serializers.IntegerField(label="上级评论id", write_only=True, required=False, allow_null=True)
    top = serializers.IntegerField(label="顶级评论id", write_only=True, required=False, allow_null=True)
    affair = serializers.IntegerField(label="帖子id", write_only=True)
    text = serializers.CharField(label="动态文字", write_only=True)

    class Meta:
        model = CommentRecord
        fields = (
            'father', 'text', 'affair', 'top',
        )


class PostCreateConnectionSerializer(serializers.ModelSerializer):
    TYPE_CHOICE = ((1, '减肥'),
                   (2, '理疗'),
                   (3, '紧致'),)
    type = serializers.ChoiceField(label="连接类别", write_only=True, choices=TYPE_CHOICE)

    class Meta:
        model = ConnectionRecord
        fields = (
            'type',
        )


class PostChangeConnectionSerializer(serializers.ModelSerializer):
    TYPE_CHOICE = ((1, '减肥'),
                   (2, '理疗'),
                   (3, '紧致'),)
    type = serializers.ChoiceField(label="连接类别", write_only=True, choices=TYPE_CHOICE)
    id = serializers.IntegerField(label="链接记录id", write_only=True)

    class Meta:
        model = ConnectionRecord
        fields = (
            'type', 'id'
        )


class PostEndConnectionSerializer(serializers.ModelSerializer):
    avg = serializers.IntegerField(label="平均心率", write_only=True)
    max = serializers.IntegerField(label="最大心率", write_only=True)
    min = serializers.IntegerField(label="最低心率", write_only=True)
    package = serializers.CharField(label="心率数据包", write_only=True)

    class Meta:
        model = ConnectionRecord
        fields = (
            'avg', 'max', 'min', 'package',
        )


class PostSaveUserBodyInfoSerializer(serializers.ModelSerializer):
    """
    BodyData上传
    """

    # PHYSIQUE_CHOICE = (
    #     (1, '隐性肥胖型'), (2, '脂肪过多型'), (3, '肥胖型'), (4, '肌肉不足型'), (5, '健康匀称型'), (6, '超重型'), (7, '消瘦型'), (8, '低脂肪型'),
    #     (9, '运动员型'))
    # bmi = serializers.FloatField(label="bmi", write_only=True)
    # fat_percentage = serializers.FloatField(label="体脂率", write_only=True)
    # water_weight = serializers.FloatField(label="水分", write_only=True)
    # visceral_fat_percentage = serializers.FloatField(label="内脏脂肪", write_only=True)
    # muscle_weight = serializers.FloatField(label="肌肉", write_only=True)
    # bmr = serializers.FloatField(label="基础代谢", write_only=True)
    # bone_weight = serializers.FloatField(label="骨质", write_only=True)
    # fat_level = serializers.IntegerField(label="肥胖等级", write_only=True)
    #
    # physique = serializers.ChoiceField(label='体型', write_only=True, choices=PHYSIQUE_CHOICE)
    # lbm = serializers.FloatField(label="去脂体重", write_only=True)
    # body_age = serializers.FloatField(label="身体年龄", write_only=True)
    # fat_control = serializers.FloatField(label="脂肪控制", write_only=True)
    # fatty_liver_risk = serializers.FloatField(label="脂肪肝风险度", write_only=True)
    # m_smm = serializers.FloatField(label="标准肌肉", write_only=True)
    # muscle_control = serializers.FloatField(label="肌肉控制", write_only=True)
    # score = serializers.FloatField(label="健康得分", write_only=True)
    # standard_fat_percentage = serializers.FloatField(label="标准体脂率", write_only=True)
    # water_percentage = serializers.FloatField(label="水分率", write_only=True)
    # weight_control = serializers.FloatField(label="体重控制量", write_only=True)
    # bone_muscle_weight = serializers.FloatField(label="骨骼肌", write_only=True)
    # protein_weight = serializers.FloatField(label="蛋白质", write_only=True)
    # sw = serializers.FloatField(label="标准体重", write_only=True)
    # weight = serializers.FloatField(label="体重", write_only=True)

    class Meta:
        model = BodyData
        fields = (
            'id', 'user', 'start_time', 'bmi',
            'fat_percentage', 'water_weight', 'visceral_fat_percentage',
            'muscle_weight',
            'bmr', 'bone_weight', 'fat_level', 'lbm', 'body_age', 'fat_control', 'fatty_liver_risk', 'm_smm',
            'muscle_control', 'physique',
            'score', 'standard_fat_percentage', 'water_percentage', 'weight_control', 'bone_muscle_weight',
            'protein_weight', 'sw', 'weight',
            'bmi_max', 'bmi_min', 'bone_muscle_weight_max', 'bone_muscle_weight_min', 'bone_weight_max',
            'bone_weight_min', 'fat_percentage_max', 'fat_weight',
            'fat_percentage_min', 'fat_weight_max', 'fat_weight_min', 'muscle_weight_max', 'muscle_weight_min',
            'protein_weight_max', 'protein_weight_min',
            'sw_max', 'sw_min', 'visceral_fat_percentage_max', 'visceral_fat_percentage_min', 'water_weight_max',
            'water_weight_min', 'weight_max', 'weight_min',
        )
